home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14138 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  4.4 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: C syntax question
  5. Date: 11 Apr 1996 23:31:35 -0700
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4kktc7INN7cp@keats.ugrad.cs.ubc.ca>
  8. References: <4ki00k$a4@mailhub.scitec.com.au>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <4ki00k$a4@mailhub.scitec.com.au>,
  12. John Saunders <johns@rd.scitec.com.au> wrote:
  13. >This is supposedly correct C, typedef names are ignored when defining
  14. >structure members. But not when defining variables so:
  15. >    byte    byte;
  16. >is an error when not inside a structure definition. Am I correct so far?
  17.  
  18. Yes, but your rationale is off. What you are doing inside the struct
  19. declaration is redeclaring an identifier that was declared in an outer scope.
  20. It's sort of like declaring a local variable x in a function when an external x
  21. also exists.
  22.  
  23. >I goal is to write a parser that handles the above correctly, all example
  24. >ANSI C parsers I have seen don't. I started playing around with mixing
  25. >typedefs and type keywords in the same declaration to see how C compilers
  26. >react. This is where it got strange. Consider the code.
  27. >
  28. >    typedef unsigned char    ubyte;
  29. >    typedef signed char        byte;
  30. >
  31. >    ubyte signed            i;
  32. >    byte unsigned            j;
  33. >    signed ubyte            k;
  34. >    unsigned byte            l;
  35.  
  36. The above declarations are wrong and require that the compiler emit a
  37. diagnostic. You can't mix a typedef name with other specifiers like ``long'',
  38. ``float'', or ``unsigned''. 
  39.  
  40. >The compilers I tried allowed the declaration of i and j (some gave warnings
  41. >and others didn't). However none liked the declaration of k and l. From this
  42. >it would seem that a typedef name is allowed only as the first token in the
  43. >declaration. All the example ANSI C grammars that I have seen allow any number
  44.  
  45. Careful: There is only *one* ANSI C grammar. It's defined in the standard.
  46.  
  47. >of typedef names or type keywords in any order. This is causing a major
  48.  
  49. You are missing the key concept that there are further constraints placed on
  50. what is an acceptable program. Not every string generated by the context free
  51. grammar is a valid C program. Some of the further constraints are semantic in
  52. nature (typechecking the use of an identifier against the declaration) but
  53. others augment the syntax in areas where placing the constraints into the
  54. grammar is not possible, or would complicate the grammar very badly.
  55.  
  56. Read the standard about the precise contstraints placed on declarations. Some
  57. of the obvious ones are that you can't have two storage types in a specifier
  58. list: ``static auto int'' doesn't make sense, though the syntax allows it. The
  59. restriction that you can't stick extra specifiers like ``signed'' or
  60. ``unsigned'' on a typedef name, even though it is an integral type, is not
  61. quite as obvious.
  62.  
  63. Check out exercise 4.10 in _Compilers Principles, Techniques and Tools_ by Aho,
  64. Sethi and Ullman: it discusses precisely the disadvantages of using the
  65. grammar to enforce certain constraints, such as disallowing conflicting or
  66. redundant declarations.  One of those disadvantages is the explosive growth in
  67. the number of grammar symbols and parser states required to handle all the
  68. possibilities and reject the violations syntactically.
  69.  
  70. >problem in being able to parse "byte byte;" correctly in the structure
  71. >definition. I.e. is the second occurance of "byte" supposed to be treated as
  72. >a typedef name or the member name?
  73.  
  74. One way: You could parse the grammar as it is written, construct a tree and
  75. then traverse it looking for constraint violations, type checking etc. You
  76. could use an n-ary tree representation, in which your declaration-specifiers
  77. would turn into, say, a node having all the type specifiers as children. You
  78. wrould write code to ensure that there is ony one storage class, type
  79. qualifiers are not duplicated, only one type specifier is given (with the
  80. exception of ``signed'' and ``unsigned'' in conjunction with ``int'', ``char''
  81. or ``long''), etc.
  82.  
  83. >Has anyone tackled this problem before?
  84.  
  85. Anyone who has ever written a C compiler, I should think. :)
  86.  
  87. Always keep in mind the distinction between syntax and semantics. The famous
  88. sentence that illustrates this is attributed to Chomsky, who was one of the
  89. first to pioneer the distinction between the two: ``Colourless green ideas
  90. sleep furiously''. Remember that and you are in good shape! :)
  91.  
  92. In C, ideas can't be declared both colourless and green. The standard prohibits
  93. it.
  94.  
  95.  
  96. -- 
  97.  
  98.